package cn.tedu.straw.redis.tag.controller;

import cn.tedu.straw.commons.vo.R;
import cn.tedu.straw.commons.vo.TagVO;
import cn.tedu.straw.redis.tag.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/v1/tags")
public class TagController {
    @Autowired
    private RedisUtils redisUtils;

    @GetMapping("")
    public R<List<Object>> a() {
        String key = "tags";
//        ListOperations<String ,Object> ops = redisTemplate.opsForList();
//        List<Object> list = ops.range(key,0,ops.size(key));

        // 从Redis服务器中读取标签列表数据
        List<Object> list = redisUtils.getList(key);
//        R<List<Object>> r = new R();
//        r.setState(R.State.SUCCESS);
//        r.setData(list);
        return R.ok(list);
    }
    // {}为占位符
    //http://localhost:8888/v1/tags/1
    @GetMapping("/{id}")
    public R<Object> getTag(@PathVariable("id") Integer id){
        Object obj = redisUtils.get("tag:"+id);
        return R.ok(obj);
    }


    //http://localhost/redis-tag/v1/tags/1/simple
    //http://localhost:8888/v1/tags/1simple
    @GetMapping("/{id}/simple")
    public Object getSimpleTag(@PathVariable("id") Integer id){
        return redisUtils.get("tag:"+id);
    }


}
